package insert

import (
	"context"

	"server_1.0/api/internal/svc"
	"server_1.0/api/internal/types"
	"server_1.0/service/student/student"
	"server_1.0/util"

	"github.com/zeromicro/go-zero/core/logx"
)

type InsertStudentLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewInsertStudentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InsertStudentLogic {
	return &InsertStudentLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InsertStudentLogic) InsertStudent(req *types.StuInsertReq) (resp *types.PostReps, err error) {

	code := util.Success
	Msg := "成功"

	ins, _ := l.svcCtx.StudentRpc.Insert(l.ctx, &student.InsertReq{
		ID:       req.Sid,
		No:       req.Sno,
		Password: req.Spwd,
		Sex:      req.Ssex,
		Name:     req.Sname,
		Age:      int64(req.Sage),
	})
	if ins.Count == 0 {
		code = util.AddError
		Msg = "失败"
	}

	return &types.PostReps{
		Code: code,
		Msg:  Msg,
		Data: map[string]interface{}{
			"res": ins.QRs,
		},
	}, nil
}
